﻿Imports Oracle.DataAccess.Client


Public Class Consec
    Private Shared lNumConsec As Integer
    Public Shared Function GetNumeroActual(ByVal TipoDoc As String, ByVal pDocEs As Int16) As Integer
        Dim Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = Conn.CreateCommand
        cmd.CommandText = "GLO_GETNUMEROCONSEC"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("PTIPODOC", OracleDbType.Varchar2).Value = TipoDoc
        cmd.Parameters.Add("PDOCES", OracleDbType.Varchar2).Value = cGeneral.DocEs(pDocEs)
        Dim PNUMERO As OracleParameter = cmd.Parameters.Add("PNUMCONSEC", OracleDbType.Int64)
        PNUMERO.Direction = ParameterDirection.Output
        Try
            Conn.Open()
            cmd.ExecuteNonQuery()
            Conn.Close()
            Return CInt(PNUMERO.Value.ToString)
        Catch ex As Exception
            cError.SalvaError(ex, "Samuel")
            Throw ex
        End Try
    End Function

    Public Shared Function ActualizaConsec(ByVal TipoDoc As String, ByVal pDocEs As Integer, ByVal Numero As Integer)
        Dim daTipoDoc = New dsGlobalTableAdapters.GLO_TIPODOCUMENTOTableAdapter
        Dim dtTipoDoc = New dsGlobal.GLO_TIPODOCUMENTODataTable
        Dim daConsec = New dsGlobalTableAdapters.GLO_CONSECUTIVOTableAdapter
        Dim dtConsec = New dsGlobal.GLO_CONSECUTIVODataTable
        Try
            dtTipoDoc = daTipoDoc.GetDataByTipoDoc(TipoDoc)
            If dtTipoDoc.Rows.Count > 0 Then
                Dim drTipoDoc As lbGlobalcom.dsGlobal.GLO_TIPODOCUMENTORow = dtTipoDoc.Rows(0)
                dtConsec = daConsec.GetDataByIdConsec(drTipoDoc.IDCONSECUTIVO)
                Dim drConsec As lbGlobalcom.dsGlobal.GLO_CONSECUTIVORow
                If dtConsec.Rows.Count = 0 Then
                    Throw New Exception("No existe tipo documento en tabla de documentos, favor verificar")
                Else
                    drConsec = dtConsec.Rows(0)
                End If
                'Dim drConsec As lbGlobalcom.dsGlobal.GLO_CONSECUTIVORow = daConsec.GetDataByIdConsec(drTipoDoc.IDCONSECUTIVO).Rows(0)
                If drConsec Is Nothing Then
                    Throw New Exception("No hay un consecutivo asociado a este tipo de documento")
                Else
                    If Numero <> 0 Then
                        If Numero > drConsec.NUMEROFINAL Then
                            Throw New Exception("Finalizo el consecutivo para este tipo de documento, verifque por favor")
                        End If
                    Else
                        If drConsec.NUMEROACTUAL >= drConsec.NUMEROFINAL Then
                            Throw New Exception("Finalizo el consecutivo para este tipo de documento, verifque por favor")
                        End If
                    End If
                    ActualizarConsec(TipoDoc, cGeneral.DocEs(pDocEs), Numero)
                    Return lNumConsec
                End If
            Else
                Throw New Exception("Tipo documento no existe")
            End If
        Catch ex As Exception
            cError.SalvaError(ex, "Samuel")
            Throw ex
        End Try
    End Function

    Private Shared Sub ActualizarConsec(ByVal TipoDoc As String, ByVal DocEs As String, ByVal Numero As Integer)
        Dim Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = Conn.CreateCommand
        cmd.CommandText = "GLO_CONSEC"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("PTIPODOC", OracleDbType.Varchar2).Value = TipoDoc
        cmd.Parameters.Add("PDOCES", OracleDbType.Varchar2).Value = DocEs
        cmd.Parameters.Add("PNUMERO", OracleDbType.Int64).Value = Numero
        Dim pNumConsec As OracleParameter = cmd.Parameters.Add("PNUMEROCONSEC", OracleDbType.Int64)
        pNumConsec.Direction = ParameterDirection.Output
        Try
            Conn.Open()
            cmd.ExecuteNonQuery()
            Conn.Close()
            lNumConsec = pNumConsec.Value
        Catch ex As Exception
            cError.SalvaError(ex, "Samuel")
            Throw ex
        End Try
    End Sub

End Class

Public Class ConsecTablas
    Private lConsec As String

    Public Sub New(ByVal Tabla As String, ByVal Campo As String, ByVal NumeroDigitos As Int16)
        Using conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Using cmd As OracleCommand = conn.CreateCommand
                cmd.CommandText = "SELECT NVL(MAX(" & Campo & "),0) AS CONSEC FROM " & Tabla
                Try
                    conn.Open()
                    Dim Consec As String = cGeneral.NZ(cmd.ExecuteScalar)
                    conn.Close()
                    If Consec IsNot DBNull.Value AndAlso IsNumeric(Consec) Then
                        lConsec = (CInt(Consec) + 1).ToString.PadLeft(NumeroDigitos, "0")
                    Else
                        lConsec = "1".PadLeft(NumeroDigitos, "0")
                    End If
                Catch ex As Exception
                    Throw ex
                End Try
            End Using
        End Using
    End Sub
    Public ReadOnly Property NumeroConsec() As String
        Get
            Return lConsec
        End Get
    End Property
End Class

Public Class cConsecutivo
    Private lDrCons As dsGlobal.GLO_CONSECUTIVORow
    Public Sub New(ByVal IdConsec As String)
        Try
            Dim daCons As New dsGlobalTableAdapters.GLO_CONSECUTIVOTableAdapter
            Dim lDtCons As dsGlobal.GLO_CONSECUTIVODataTable = daCons.GetDataByIdConsec(IdConsec)
            If lDtCons.Rows.Count = 0 Then
                Throw New Exception("No existe consecutivo en tabla de consecutivos favor verificar")
            Else
                lDrCons = lDtCons.Rows(0)
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public ReadOnly Property idConsec() As String
        Get
            Return lDrCons.IDCONSECUTIVO
        End Get
    End Property
    Public ReadOnly Property Descripcion() As String
        Get
            Return lDrCons.DESCRIPCION
        End Get
    End Property
    Public ReadOnly Property NumeroActual() As Integer
        Get
            Return lDrCons.NUMEROACTUAL
        End Get
    End Property
    Public ReadOnly Property Numeroinicial() As Integer
        Get
            Return lDrCons.NUMEROINICIAL
        End Get
    End Property
    Public ReadOnly Property Numerofinal() As Integer
        Get
            Return lDrCons.NUMEROFINAL
        End Get
    End Property
    Public ReadOnly Property Numeroresolucion() As String
        Get
            Return lDrCons.NUMERORESOLUCION
        End Get
    End Property
    Public ReadOnly Property Fecharesolucion() As DateTime
        Get
            Return lDrCons.FECHARESOLUCION
        End Get
    End Property
    Public ReadOnly Property Prefijo() As String
        Get
            Return lDrCons.PREFIJO
        End Get
    End Property
End Class